﻿
function init() {
    comForm.bind(null, 'USER_SEX', { data: DataDict['data_baseusersex'], textField: 'text', valueField: 'value' }, 3);
    comForm.bind(null, 'SPACE_STATUS', { data: DataDict['data_space_status'], textField: 'text', valueField: 'value' }, 3);
    
        loadOrgData();
        loadRoleData();
//        $('#userinfo').tabs({
//            border: false,
//            onSelect: function (id) {
//              //  if (id == "t2") { loadOrgData(); }
//            }
        //        });
        if ($.getPara('userid') != '') {
            inituser();
        }
    }


//#region 加载营业部数据
function loadOrgData() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryOrganizeList",
        success: function (data) {
            if (data && data.length > 0) {

                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ORGANIZATION_ID;

                        obj.text = data[i].ORGANIZATION_NAME;
                        getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                        _data.push(obj);
                    }
                }


                $('#ORGANIZATION_ID').combotree({ cascadeCheck:false });
                $('#ORGANIZATION_ID').combotree('loadData', _data);
                //绑定部门
                if ($.getPara('userid') != '') {
                    $.ajaxHandler({
                        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
                        urlType: "GetStaffOrganize",
                        model: function () {
                            return $.getPara('userid');
                        },
                        success: function (_orgdata) {
                            if (_orgdata && _orgdata.length > 0) {
                                var _dataorgarr = [];
                                for (var i = 0; i < _orgdata.length; i++) {
                                    _dataorgarr.push(_orgdata[i].ORGANIZATION_ID);
                                }
                                $('#ORGANIZATION_ID').combotree('setValues', _dataorgarr);
                            }
                        }
                    });
                }

            }

        }
    });

    function getsubOrg(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ORGANIZATION_ID;
                    obj.text = _d[i].ORGANIZATION_NAME;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubOrg(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}

//#endregion



//#region 加载角色数据
function loadRoleData() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "RolesList",
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ROLES_ID;
                        obj.text = data[i].ROLES_NAME;
                        obj.SORTCODE = data[i].SORTCODE;
                        obj.CREATEUSERNAME = data[i].CREATEUSERNAME;
                        obj.CREATEDATE = data[i].CREATEDATE;
                        obj.MODIFYUSERNAME = data[i].MODIFYUSERNAME;
                        obj.MODIFYDATE = data[i].MODIFYDATE
                        obj.ROLE_RESTRICTION = data[i].ROLE_RESTRICTION;
                        obj.DELETEMARK = data[i].DELETEMARK;
                        getsubRole(data, data[i].ROLES_ID, obj);
                        _data.push(obj);
                    }
                }
                $('#ORLEList').tree('loadData', _data);
                bindroleright();
            }

        }
    });

    function getsubRole(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ROLES_ID;
                    obj.DELETEMARK = _d[i].DELETEMARK;
                    obj.text = _d[i].ROLES_NAME;
                    obj.ROLE_RESTRICTION = _d[i].ROLE_RESTRICTION;
                    obj.CREATEUSERNAME = _d[i].CREATEUSERNAME;
                    obj.CREATEDATE = _d[i].CREATEDATE;
                    obj.MODIFYUSERNAME = _d[i].MODIFYUSERNAME;
                    obj.MODIFYDATE = _d[i].MODIFYDATE
                    obj.SORT_CODE = _d[i].SORT_CODE;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubRole(_d, _sdata[j].ROLES_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

function inituser() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "GetUserByID",
        model: function () {
            return $.getPara('userid');
        },
        success: function (data) {
            if (data) {
                for (var key in data) {
                    if ($('#' + key).length > 0) {
                        if (key == "USER_PWD") continue; //不显示加密密码
                            $('#' + key).val(data[key]);
                        if (key == "SPACE_SIZE")
                            $('#' + key).val(data[key]);
                        if (key == "TELPHONE")
                            $('#' + key).val(data[key]);
                        if (key == "INTEGRAL")
                            $('#' + key).val(data[key]);
                    }
                }
            }
        }
    });
}

//#region 编辑
function savecheck() {
    if ($('#USER_CODE').val() == '') {
        alert(lang.Employee_number_can_not_be_empty);
        return false;
    } else if ($('#USER_NAME').val() == '') {
        alert(lang.Employee_username_can_not_be_empty);
        return false;
    } else if ($('#USER_ACCOUNT').val() == '') {
        alert(lang.Employee_account_can_not_be_empty);
        return false;
    }  else if ($('#ORGANIZATION_ID').combotree('getValues').length == 0) {
        alert(lang.Department_can_not_be_empty);
        return false;
    }

    if ($.getPara('userid') == '' && $('#USER_PWD').val() == '') {
        alert(lang.User_password_can_not_be_empty);
        return false;
    }
    return true;
}
function save() {
    if (savecheck()) {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "EditUserInfo",
            model: function () {
                var obj = {};
                if ($.getPara('userid') != '') {
                    obj.USER_ID = $.getPara('userid');
                    obj.action = 1;
                }
                else {
                    obj.action = 2;
                }
                var arr = $('#ORGANIZATION_ID').combotree('getValues');
                var orgIDlist = [];
                for (var i = 0; i < arr.length; i++) {
                    orgIDlist.push({ OrgID: arr[i] });
                }
                obj.orgID = orgIDlist;

                obj.USER_CODE = $('#USER_CODE').val();
                obj.USER_NAME = $('#USER_NAME').val();
                obj.USER_ACCOUNT = $('#USER_ACCOUNT').val();
                obj.EMAIL = $('#EMAIL').val();
                if ($.getPara('userid') != '') {
                    if ($.trim($('#USER_PWD').val()) != '') {
                        obj.USER_PWD = hex_md5($('#USER_PWD').val());
                    }
                }
                else {
                    obj.USER_PWD = hex_md5($('#USER_PWD').val());
                }
                obj.USER_SEX = $('#USER_SEX').val();
                obj.SPACE_STATUS = $('#SPACE_STATUS').val();
                obj.TELPHONE = $('#TELPHONE').val();
                obj.SPACE_SIZE = $('#SPACE_SIZE').val();
                obj.SPACE_CURE_USE = $('#SPACE_CURE_USE').val();
                obj.INTEGRAL = $('#INTEGRAL').val();
                obj.TITLE = $('#TITLE').val();
                obj.USER_REMARK = $('#USER_REMARK').val();
                
                //角色
                var _roleList = [];
                var rolelist = $('#ORLEList').tree('getChecked');
                if (rolelist && rolelist.length > 0) {
                    for (var i = 0; i < rolelist.length; i++) {
                        _roleList.push(rolelist[i].id);
                    }
                }
                obj.RoleList = _roleList;
                return $.toJSON(obj);
            },
            success: function (data) {
                if (data) {
                    if ($.getPara('userid') != '') {
                        //                        $.alert(lang.Complete_the_user_information_edit, null, function () { parent.userlist.search(); parent.$.closePage(); });
                        alert(lang.Complete_the_user_information_edit);
                        parent.userlist.search();
                        parent.$.closePage();
                    } else {
                        //$.alert(lang.Complete_the_user_information_add, null, function () { parent.userlist.search(); parent.$.closePage(); });
                        alert(lang.Complete_the_user_information_add);
                        parent.userlist.search();
                        parent.$.closePage();
                    }
                }
            }
        });
    }
}




function exit() {
    parent.$.closePage();
}
//#endregion

//#region 角色
function bindroleright() {
    if ($.getPara('userid') != '') {
        $.ajaxHandler({
            url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
            urlType: "QueryUserRole",
            model: function () {
                return $.getPara('userid');
            },
            success: function (data) {
                if (data && data.length > 0) {
                //绑定check
                    for (var i = 0; i < data.length; i++) {
                        var node = $('#ORLEList').tree('find', data[i].ROLES_ID);
                        $('#ORLEList').tree('check', node.target);
                    }  
                }
            }
        });
        
    }
}
//#endregion